#pragma once

#include "actorbase.h"
#include "scene.h"
#include "actorproperty.h"
#include "map.h"
#include "actorfactory.h"
#include <QUuid>

namespace CoreBase
{
	ActorBase::ActorBase():_withNoDrawableId(""),_belongMap(NULL),_withNoDrawableName("undefine")
		,_actorFactory(NULL)
	{
		setClassName("ActorBase");
	}

	ActorBase::ActorBase(const ActorBase& rhs)
	{}

	ActorBase::~ActorBase()
	{}

	ActorBase& ActorBase::operator=(const ActorBase& rhs)
	{
		return *this;
	}

	void ActorBase::init()
	{
		createDrawable();
		buildPropertyMap();
	}

	void ActorBase::setClassName(const QString& name)
	{
		_className = name;
		_classHierarchyType.push_back(_className);
	}

	const QString& ActorBase::getId()
	{
		if(_drawable.valid())
			return _drawable->getId();
		else if(_withNoDrawableId == "")
		{
			_withNoDrawableId = QUuid::createUuid().toString();
		}
		return _withNoDrawableId;
	}

	void ActorBase::setId(const QString& newid)
	{
		if(_drawable.valid())
			_drawable->setId(newid);
		else
			_withNoDrawableId = newid;
	}

	const QString& ActorBase::getName()
	{
		if(_drawable.valid())
			return _drawable->getName();
		else
			return _withNoDrawableName;
	}

	void ActorBase::setName(const QString& name)
	{
		if(_drawable.valid())
			_drawable->setName(name);
		else
			_withNoDrawableName = name;
	}

	bool ActorBase::isGhostProxy() const
	{
		return false;
	}

	Drawable* ActorBase::getDrawable()
	{
		return _drawable.get();
	}

	const Drawable* ActorBase::getDrawable() const
	{
		return _drawable.get();
	}

	void ActorBase::setDrawable(Drawable* actor)
	{
		_drawable = actor;
	}

	osg::ref_ptr<ActorBase> ActorBase::clone()
	{
		osg::ref_ptr<ActorBase> copy;
		copy = getActorFactory()->createActorByDataType(getClassName());
		copy->setName(getName());
		copy->copyPropertiesFrom(*this);
		copy->setBelongMap(_belongMap);
		return copy;
	}

	ActorFactory* ActorBase::getActorFactory()
	{
		return _actorFactory;
	}

	void ActorBase::buildPropertyMap()
	{}

	Map* ActorBase::getBelongMap()
	{
		return _belongMap;
	}

	void ActorBase::setBelongMap(CoreBase::Map* map)
	{
		_belongMap = map;
	}

	void ActorBase::registerActorAsProperty(ActorBase* actor,QString propertyname)
	{
		_childPropertyMap[actor] = propertyname;
	}

	void ActorBase::unRegisterActorAsProperty(ActorBase* actor)
	{
		QMap<ActorBase*,QString>::Iterator iter = _childPropertyMap.find(actor);
		if(iter != _childPropertyMap.end())
			_childPropertyMap.erase(iter);
	}

	QMap<ActorBase*,QString> ActorBase::getChildActorAsProperty()
	{
		return _childPropertyMap;
	}
}